package com.bootdo.backlist.controller;

import java.util.List;
import java.util.Map;

import com.bootdo.common.enums.ResultEnum;
import com.bootdo.common.interfaces.Add;
import com.bootdo.common.interfaces.Update;
import com.bootdo.backlist.converter.ConvertDomainUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bootdo.backlist.dto.InfoDTO;
import com.bootdo.backlist.form.InfoForm;
import com.bootdo.backlist.vo.InfoVO;
import com.bootdo.backlist.service.InfoService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 基础信息
 * 
 * @author zengcx
 * @email 13851161069@163.com
 * @date 2019-04-15 14:58:08
 */
 
@Controller
@Slf4j
@RequestMapping("/backlist/info")
public class InfoController {
	@Autowired
	private InfoService infoService;
	
	@GetMapping()
	@RequiresPermissions("backlist:info:info")
	String Info(){
	    return "backlist/info/info";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("backlist:info:info")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<InfoVO> infoList = ConvertDomainUtils.convertList(infoService.list(query),InfoVO.class);
		int total = infoService.count(query);
		PageUtils pageUtils = new PageUtils(infoList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("backlist:info:add")
	String add(){
	    return "backlist/info/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("backlist:info:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		InfoDTO info = infoService.get(id);
		model.addAttribute("info", info);
	    return "backlist/info/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("backlist:info:add")
	public R save(@Validated(Add.class) InfoForm infoForm, BindingResult bindingResult){
	    if (bindingResult.hasErrors()){
	        log.error("【传参错误】,infoForm={}",infoForm);
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
		return infoService.save(ConvertDomainUtils.convertObject(infoForm,InfoDTO.class)) ;
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("backlist:info:edit")
	public R update(@RequestBody @Validated(Update.class) InfoForm infoForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("【传参错误】,infoForm={}",infoForm);
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
		return  infoService.update(ConvertDomainUtils.convertObject(infoForm,InfoDTO.class)) ;
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("backlist:info:remove")
	public R remove( Integer id){
	    return infoService.remove(id);
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("backlist:info:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		return infoService.batchRemove(ids);
	}
	
}
